package ru.ifmo.ct.network.brafs;

import ru.ifmo.ct.network.brafs.client.BraServer;
import ru.ifmo.ct.network.brafs.client.BraServerClient;

import java.io.*;
import java.util.List;

/**
 * @author Alex Vikharev vikharev@yandex-team.ru
 *         created 18.04.12
 */
public class Main {
    private static final String USAGE = "Usage: java -jar braclient.jar command server_host server_port remote_path [local_path]";  //TODO: write usage

    public static void main(String[] args) throws IOException {
        if (args.length < 3 || args.length > 5) {
            System.out.println(USAGE);
            return;
        }

        final BraServer server = new BraServerClient(args[1], Integer.parseInt(args[2]));
        try {
            server.connect();
            switch (args[0].toLowerCase()) {
                case "put":
                    writeFile(args, server);
                    break;
                case "get":
                    readFile(args, server);
                    break;
                case "ls":
                    listFiles(args, server);
                    break;
                default:
                    System.out.println(USAGE);
            }
        } catch (IOException e) {
            System.err.println("Error connecting to server: " + e.getLocalizedMessage());
        } finally {
            server.disconnect();
        }
    }

    private static void writeFile(final String[] args, final BraServer server) {
        if (args.length == 5) {
            final String remotePath = args[3];
            final String localPath = args[4];
            final byte[] buffer = new byte[1024 * 1024];
            final File file = new File(localPath);
            try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
                try (OutputStream outputStream = server.getOutputStream(remotePath)) {
                    int size = 0;
                    while (size != -1) {
                        size = inputStream.read(buffer);
                        if (size != -1) {
                            outputStream.write(buffer, 0, size);
                        }
                    }
                }
            } catch (Exception e) {
                System.err.println("Error: " + e.getLocalizedMessage());
            }
        }
    }

    private static void readFile(final String[] args, final BraServer server) {
        final String remotePath = args[3];
        final String localPath = args[4];
        final byte[] buffer = new byte[1024 * 1024];
        try (InputStream inputStream = new BufferedInputStream(server.getInputStream(remotePath))) {
            try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localPath))) {
                int size = 0;
                while (size != -1) {
                    size = inputStream.read(buffer);
                    if (size != -1) {
                        outputStream.write(buffer, 0, size);
                    }
                }
            }
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    private static void listFiles(final String[] args, final BraServer server) throws IOException {
        final String prefix = args.length >= 4 ? args[3] : "";
        final List<String> files = server.listFiles(prefix);
        for (final String file : files) {
            System.out.println(file);
        }
    }
}
